package cn.ac.cintcm.prespring.cluster.name;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.solr.core.Config;

import com.hs.log.HsLogger;

/**
 * name similarity based on keyword frequency and dictionay
 * 
 * @author CaoMudan
 * 
 */
public class NameSimBasedOnFreq extends NameSimFj {
	private static final HsLogger logger = HsLogger.getLogger(NameSimBasedOnFreq.class.getName());
	
	public NameSimBasedOnFreq(float simRate) {
		super(simRate);
		super.setOutputText(outputText);
	}

	private String fjUrl = Config.getInstanceDir()+ "data/tempFiles/PRES_FJ_Name_Word_Freq_Src.txt";
	private String outputText = Config.getInstanceDir()+ "data/similars/PRES_FJ_NameSimFreq.txt";
	private static int MAXFREQ = 680;

	public static void main(String[] args) throws Exception {
		long start=System.currentTimeMillis();
		NameSimBasedOnFreq simFj = new NameSimBasedOnFreq(0.8f);
		simFj.writeSimToText(simFj.calculate());
		long end=System.currentTimeMillis();
		System.out.println("fangji name similar --- use:"+(end-start));
	}
	
	public static void generateNAMEsimilar() throws Exception{
		long start=System.currentTimeMillis();
		
		logger.info("to generate PRES_FJ_Name_Word.txt");
		FjName2Word.generateWordText(FjName2Word.readFromText());
		
		logger.info("to generate PRES_FJ_Name_Word_Freq_Src.txt");
		WordFrequency.calcWordFreqWithSource(WordFrequency.readWordFromText());
		
		logger.info("to generate PRES_FJ_NameSimFreq.txt");		
		NameSimBasedOnFreq simFj = new NameSimBasedOnFreq(0.8f);
		simFj.writeSimToText(simFj.calculate());
		long end=System.currentTimeMillis();
		
		logger.info("fangji name similar --- use:"+(end-start));
		
	}

	/**
	 * @return map(FjName,fangjiIds)
	 */
	public Map<String, List<String>> calculate() throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(fjUrl), "gb2312"));
		String line = "";

		Map<String, List<String>> map = new HashMap<String, List<String>>();
		Set<String> wordDict = Dictionary.getWordDict();
		int count = 0;
		while ((line = reader.readLine()) != null) {
			String[] s = line.split("    ");
			if (s.length == 3) {
				int freq = new Integer(s[1]);
				if (freq < 10) {
					continue;
				}
				String fjName = s[0];
				String[] fjIds = s[2].split(";");
				if (fjIds.length > 1) {
					if (wordDict.contains(fjName)) {
						addSimilar(map, fjIds);
					} else {
						count++;
					}
				}
				//System.out.println(fjName + " ----- " + s[2]);
			}
		}
		System.err.println("count is"+count);//count is 682
		reader.close();
		return map;
	}

	private void addSimilar(Map<String, List<String>> map, String[] fjIds) {
		for (int i = 0; i < fjIds.length; i++) {
			String sameNameFjIds = fjIds[i];
			String[] sameName=sameNameFjIds.split(",");
			for(int j=0;j<sameName.length;j++){
				String fjId=sameName[j];
				for(int k=0;k<fjIds.length;k++){
					if(k!=i){
						if(map.get(fjId)!=null){
							map.get(fjId).add(fjIds[k].split(",")[0]);
						}else{
							List<String> result = new ArrayList<String>();
							result.add(fjIds[k].split(",")[0]);
							map.put(fjId, result);
						}
					}
				}
			}
		}
	}

}
